/*
package com.lagou.config;*/
/**
 * Created by IntelliJ IDEA.
 * User: Yangdx.
 * Date: 2020/8/5.
 * Time: 13:20.
 * Tags: Code, we are serious.
 *//*


import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;

*/
/**
 * @author
 * @create 2020-08-05 13:20
 **//*

@Component
public class NacosDynamicRouteService implements ApplicationEventPublisherAware {

    private String dataId = "gateway-router";

    private String group = "DEFAULT_GROUP";

    @Value("${spring.cloud.nacos.config.server-addr}")
    private String serverAddr;

    @Resource
    private RouteDefinitionWriter routeDefinitionWriter;

    private ApplicationEventPublisher applicationEventPublisher;

    private static final List<String> ROUTE_LIST = new ArrayList<>();

    @PostConstruct
    public void dynamicRouteByNacosListener() {
        try {
            ConfigService configService = NacosFactory.createConfigService(serverAddr);
            String content = configService.getConfig(dataId, group, 5000);
            List<RouteDefinition> gatewayRouteDefinitions = getListByStr(str);
            configService.addListener(dataId, group, new Listener() {
                @Override
                public void receiveConfigInfo(String configInfo) {
                    clearRoute();
                    try {
                        for (RouteDefinition routeDefinition : gatewayRouteDefinitions) {
                            addRoute(routeDefinition);
                        }
                        publish();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public Executor getExecutor() {
                    return null;
                }
            });
        } catch (NacosException e) {
            e.printStackTrace();
        }
    }

    private void clearRoute() {
        for (String id : ROUTE_LIST) {
            this.routeDefinitionWriter.delete(Mono.just(id)).subscribe();
        }
        ROUTE_LIST.clear();
    }

    private void addRoute(RouteDefinition definition) {
        try {
            routeDefinitionWriter.save(Mono.just(definition)).subscribe();
            ROUTE_LIST.add(definition.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void publish() {
        this.applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this.routeDefinitionWriter));
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    */
/**
     * 解析路由
     *
     * @param content
     * @return
     *//*

    private List<RouteDefinition> getListByStr(String content) {
        if (StringUtils.isNotEmpty(content)) {
            return JSONObject.parseArray(content, RouteDefinition.class);
        }
        return new ArrayList<>(0);
    }

    private static String str = "[{\n" +
            "    \"id\": \"service-user-router\",\n" +
            "    \"order\": 0,\n" +
            "    \"predicates\": [{\n" +
            "        \"args\": {\n" +
            "            \"pattern\": \"/user/**\"\n" +
            "        },\n" +
            "        \"name\": \"Path\"\n" +
            "    }],\n" +
            "    \"uri\": \"lb://user-service\"\n" +
            "},\n" +
            "{\n" +
            "    \"id\": \"service-code-router\",\n" +
            "    \"order\": 0,\n" +
            "    \"predicates\": [{\n" +
            "        \"args\": {\n" +
            "            \"pattern\": \"/code/**\"\n" +
            "        },\n" +
            "        \"name\": \"Path\"\n" +
            "    }],\n" +
            "    \"uri\": \"lb://service-code\"\n" +
            "},\n" +
            "{\n" +
            "    \"id\": \"service-email-router\",\n" +
            "    \"order\": 0,\n" +
            "    \"predicates\": [{\n" +
            "        \"args\": {\n" +
            "            \"pattern\": \"/email/**\"\n" +
            "        },\n" +
            "        \"name\": \"Path\"\n" +
            "    }],\n" +
            "    \"uri\": \"lb://mail-service\"\n" +
            "}]";
}
*/
